package com.qire.manhua.view;

import android.content.Context;
import android.util.AttributeSet;

import com.qire.manhua.R;

/**
 * 可清除的复合编辑文本框
 */
public class CanClearCompoundEventEditText extends CompoundEventEditText {
    private EventDrawable clearBtn;
    public CanClearCompoundEventEditText(Context context) {
        super(context);
        init();
    }

    public CanClearCompoundEventEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CanClearCompoundEventEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if(clearBtn==null)
            return;
        if(text.length()<=0)
            clearBtn.setVisible(false);
        else
            clearBtn.setVisible(true);
    }

    public void init(){
        clearBtn = addEventDrawable(R.drawable.app_compound_icon_del,"clearBtn");
        clearBtn.setVisible(false);
        setCompoundEventListener("clearBtn",clearBtnEventListener);
    }

    private CompoundEventListener clearBtnEventListener = (view,eventDrawable) -> {
        view.setText("");
        return true;
    };

}

